#pragma once
#include <deque>
#include <vector>
#include <random>
#include <memory>
#include <string>
#include <algorithm>
#include <cmath>
#include <iostream>
/**
 * 可调参数区
 */
#define LEARNING_RATE 0.01
#define STDP_WINDOW 20
#define MAX_WEIGHT 1.0
#define MIN_WEIGHT 0.0
#define CONNECTION_RADIUS 50.0
#define CONNECTION_PROBABILITY 0.3
#define MAX_CONNECTIONS 50
#define ACTIVATION_THRESHOLD 10.0
#define REFRACTORY_STEPS 3
#define DECAY_RATE 0.9
#define INHIBITORY_THRESHOLD 8.0
#define INHIBITORY_STRENGTH 12.0
#define MODULATORY_THRESHOLD 15.0
#define MODULATORY_RANGE 80.0
#define ATTENTION_SIGNAL 8.0
#define REWARD_SIGNAL 10.0
#define ALERT_SIGNAL 15.0
#define HISTORY_LENGTH 100
#define PRUNE_THRESHOLD 0.05
#define REORGANIZE_INTERVAL 50

enum NeuronType {
    EXCITATORY,
    INHIBITORY,
    MODULATORY
};

struct Position3D {
    double x, y, z;
    Position3D(double x = 0.0, double y = 0.0, double z = 0.0);
    double distance_to(const Position3D& other) const;
};

class SimpleNeuron;

struct Connection {
    SimpleNeuron* target;
    double weight;
    std::deque<int> pre_activations;
    Connection(SimpleNeuron* t, double w = 0.5);
};

struct SimpleNeuron {
    static int next_id;
    static int global_step;

    int neuron_id;
    Position3D position;
    NeuronType type;
    double activation_level;
    int last_activation_step;
    std::deque<int> activation_history;
    std::vector<std::unique_ptr<Connection>> outputs;
    std::deque<double> input_history;
    double modulatory_state;
    int last_modulatory_event;

    mutable std::random_device rd;
    mutable std::mt19937 gen;
    mutable std::uniform_real_distribution<> dis;

    SimpleNeuron(double x = 0.0, double y = 0.0, double z = 0.0, NeuronType t = EXCITATORY);
    ~SimpleNeuron();

    // 接收输入刺激
    void receive_input(double strength, SimpleNeuron* source = nullptr);

    // 检查是否应该激活
    bool should_activate() const;

    // 执行激活
    void activate();

    // 兴奋性神经元激活
    void activate_excitatory();

    // 抑制性神经元激活
    void activate_inhibitory();

    // 调制性神经元激活
    void activate_modulatory();

    // 确定调制信号类型和强度
    double determine_modulation_signal();

    // 接收调制信号
    void receive_modulation(double modulation, SimpleNeuron* source);

    // 突触可塑性调整
    void adjust_weights();

    // 空间连接形成
    void form_connections(std::vector<SimpleNeuron*>& all_neurons);

    // 获取与目标神经元的连接概率
    double get_connection_probability(SimpleNeuron* target);

    // 移除弱连接
    void prune_weak_connections();

    // 每步更新
    void update();

    // 信息获取结构体
    struct NeuronInfo {
        int id;
        NeuronType type;
        double activation_level;
        int connections_count;
        double average_weight;
        int recent_activations;
        double modulatory_state;
    };

    // 获取神经元信息
    NeuronInfo get_info() const;

    // 静态方法
    static void advance_global_step();
    static int get_global_step();
    static void reset_global_step();

    // 清理历史
    void cleanup_history();

    // 获取神经元类型名称
    std::string get_type_name() const;
};

struct SimpleNeuralNetwork {
    std::vector<std::unique_ptr<SimpleNeuron>> neurons;
    SimpleNeuralNetwork();

    // 添加神经元到网络
    void add_neuron(double x, double y, double z, NeuronType type = EXCITATORY);

    // 创建随机分布的神经网络
    void create_random_network(int num_neurons, double space_size = 100.0);

    // 重新组织连接
    void reorganize_connections();

    // 仿真一步
    void simulate_step();

    // 给随机神经元输入刺激
    void add_stimulus(double strength = 15.0);

    // 输出网络状态
    void print_status() const;

    size_t size() const;
};
